import axios from 'axios'
import nprogress from 'nprogress'
import { ElMessageBox } from 'element-plus'
import router from '@/router/index.js'
let http = axios.create({
    baseURL:"/api",
    timeout:1000*10,
})

http.interceptors.request.use(config=>{
    // console.log(config);
    // let token = sessionStorage.getItem('token');
    // config.headers.authorization = token
    nprogress.start()
    nprogress.set(0.2)
    let token = sessionStorage.getItem('token');
    config.headers.authorization = token
    return config
})

http.interceptors.response.use(config=>{
    nprogress.done()
  if (config.data.state) {
    
    return config
} else {
  
    ElMessageBox.alert(config.data.msg, '提示', {
        type: 'error',
    })
    if (config.data.code === '1004' || config.data.code === '10022') {
        router.replace('/login')
        sessionStorage.removeItem('token')
        window.location.reload()
    }

    return Promise.reject(config.data.msg)
}
})

export default http